package com.pair.micro.fee.modules.charge.controller;


import com.pair.micro.common.constant.MicroBusinessError;
import com.pair.micro.common.exception.MicroBusinessException;
import com.pair.micro.common.operatelog.annotations.OperateLog;
import com.pair.micro.common.operatelog.enums.OperateTypeEnum;
import com.pair.micro.common.response.MicroResult;
import com.pair.micro.common.utils.PageResult;
import com.pair.micro.fee.modules.charge.convert.ChargeProjectConvert;
import com.pair.micro.fee.modules.charge.entity.ChargeProjectEntity;
import com.pair.micro.fee.modules.charge.entity.RoomOwnerEntity;
import com.pair.micro.fee.modules.charge.query.ChargeProjectQuery;
import com.pair.micro.fee.modules.charge.service.ChargeDetailService;
import com.pair.micro.fee.modules.charge.service.ChargeProjectService;
import com.pair.micro.fee.modules.charge.service.ChargeReceiveService;
import com.pair.micro.fee.modules.charge.vo.ChargeDetailVO;
import com.pair.micro.fee.modules.charge.vo.ChargeProjectVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.AllArgsConstructor;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;


@RestController
@RequestMapping("fx/detail")
@Tag(name = "收取详情")
@AllArgsConstructor
public class ChargeDetailController {

    private final ChargeProjectService chargeProjectService;

    private final ChargeDetailService chargeDetailService;

    private final ChargeReceiveService chargeReceiveService;
    @GetMapping("page")
    @Operation(summary = "分页")
    @PreAuthorize("hasAuthority('fee:project:list')")
    public MicroResult<PageResult<ChargeProjectVO>> page(@ParameterObject @Valid ChargeProjectQuery chargeProjectQuery){
        PageResult<ChargeProjectVO> page = chargeProjectService.page(chargeProjectQuery);
        return MicroResult.ok(page);
    }


    @PostMapping(value = "save")
    @Operation(summary = "保存")
    @OperateLog(type = OperateTypeEnum.INSERT)
//  @PreAuthorize("hasAuthority('fee:project:save')")
    public MicroResult<String> save(@RequestBody @Valid ChargeDetailVO vo) {

        int receiveCount = chargeReceiveService.getReceiveCount(vo);
        if(receiveCount==vo.getReceiveVO().size()){ //如果通过选择的账单id查询出数据库里面的收费中的账单数量和传来的账单数量比较，如果不相等说明重复缴费了
            chargeDetailService.charge(vo);
        }else {
            throw new MicroBusinessException(MicroBusinessError.BIS_50003);
        }
        return MicroResult.ok();
    }

    @GetMapping("{id}")
    @Operation(summary = "信息")
    @PreAuthorize("hasAuthority('fee:project:info')")
    public MicroResult<ChargeProjectVO> get(@PathVariable("id") String id) {
        ChargeProjectEntity entity = chargeProjectService.getById(id);
        return MicroResult.ok( ChargeProjectConvert.INSTANCE.convert(entity));
    }

    @DeleteMapping
    @Operation(summary = "删除")
    @OperateLog(type = OperateTypeEnum.DELETE)
    @PreAuthorize("hasAuthority('fee:project:delete')")
    public MicroResult<String> delete(@RequestBody List<Long> idList) {
        chargeProjectService.delete(idList);
        return MicroResult.ok();
    }

    @PostMapping("import")
    @Operation(summary = "导入用户")
    @OperateLog(type = OperateTypeEnum.IMPORT)
    @PreAuthorize("hasAuthority('fee:project:import')")
    public MicroResult<String> importExcel(@RequestParam("file") MultipartFile file) {
        if (file.isEmpty()) {
            return MicroResult.error("请选择需要上传的文件");
        }
//        chargeConfigService.importByExcel(file, passwordEncoder.encode("123456"));

        return MicroResult.ok();
    }

    @GetMapping("export")
    @Operation(summary = "导出用户")
    @OperateLog(type = OperateTypeEnum.EXPORT)
    @PreAuthorize("hasAuthority('fee:config:export')")
    public void export() {
//        chargeConfigService.export();
    }
}
